package com.bcmusic.point.controller;

import com.bcmusic.point.config.TokenDecode;
import com.bcmusic.point.pojo.UserAsset;
import com.bcmusic.point.pojo.UserMbLog;
import com.bcmusic.point.pojo.UserSignIn;
import com.bcmusic.point.service.UserAssetService;
import com.bcmusic.point.service.UserMbLogService;
import com.bcmusic.point.service.UserSignInService;
import com.bcmusic.point.service.impl.UserSignInServiceImpl;
import com.github.pagehelper.PageInfo;
import entity.DateUtil;
import entity.IdWorker;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/userSignIn")
@CrossOrigin
public class UserSignInController {

    @Autowired
    private UserSignInService userSignInService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private UserMbLogService userMbLogService;

    @Autowired
    private UserAssetService userAssetService;

    private SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    private TokenDecode tokenDecode;


    /***
     * UserSignIn分页条件搜索实现
     * @param userSignIn
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false)  UserSignIn userSignIn, @PathVariable  int page, @PathVariable  int size){
        //调用UserSignInService实现分页条件查询UserSignIn
        PageInfo<UserSignIn> pageInfo = userSignInService.findPage(userSignIn, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * UserSignIn分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用UserSignInService实现分页查询UserSignIn
        PageInfo<UserSignIn> pageInfo = userSignInService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param userSignIn
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<UserSignIn>> findList(@RequestBody(required = false)  UserSignIn userSignIn){
        //调用UserSignInService实现条件查询UserSignIn
        List<UserSignIn> list = userSignInService.findList(userSignIn);
        return new Result<List<UserSignIn>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        //调用UserSignInService实现根据主键删除
        userSignInService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改UserSignIn数据
     * @param userSignIn
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  UserSignIn userSignIn,@PathVariable Long id){
        //设置主键值
        userSignIn.setId(id);
        //调用UserSignInService实现修改UserSignIn
        userSignInService.update(userSignIn);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增UserSignIn数据
     * @param userSignIn
     * @return
     */
    @PostMapping
    public Result add(@RequestBody   UserSignIn userSignIn){
        //调用UserSignInService实现添加UserSignIn
        userSignInService.add(userSignIn);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询UserSignIn数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<UserSignIn> findById(@PathVariable Long id){
        //调用UserSignInService实现根据主键查询UserSignIn
        UserSignIn userSignIn = userSignInService.findById(id);
        return new Result<UserSignIn>(true,StatusCode.OK,"查询成功",userSignIn);
    }

    /***
     * 查询UserSignIn全部数据
     * @return
     */
    @GetMapping
    public Result<List<UserSignIn>> findAll(){
        //调用UserSignInService实现查询所有UserSignIn
        List<UserSignIn> list = userSignInService.findAll();
        return new Result<List<UserSignIn>>(true, StatusCode.OK,"查询成功",list) ;
    }

    /**
     * 查询签到情况
     */
    @GetMapping("query")
    public Result<UserSignIn> query (){
        String userNum = tokenDecode.getUserInfo().get("id");
        UserSignIn signIn=new UserSignIn();
        signIn.setUserNumber(userNum);
        List<UserSignIn> list = userSignInService.findList(signIn);

        if(list.isEmpty())
        {
            //如果没有签到过就手动添加一条空的签到记录
            signIn.setId(idWorker.nextId());
            signIn.setUserNumber(userNum);
            signIn.setContinueDays(0);
            userSignInService.add(signIn);
        }else
        {
            signIn=list.get(0);
        }

        return new Result<>(true,StatusCode.OK,"查询签到情况成功",signIn);
    }

    /**
     * 用户签到,更新用户mb积分
     */
    @GetMapping("signIn")
    public Result<UserSignIn> singIn()
    {
        //更新签到表
        String userNum = tokenDecode.getUserInfo().get("id");
        String mb=null;
        UserSignIn userSignIn=new UserSignIn();
        userSignIn.setUserNumber(userNum);
        userSignIn=userSignInService.findList(userSignIn).get(0);
        //判断昨天是否签到
        Date lastSignDate=userSignIn.getUpdateTime();
        if(DateUtil.isSignYesterday(lastSignDate))
        {
            userSignIn.setContinueDays(userSignIn.getContinueDays()+1);
            mb=String.valueOf(userSignIn.getContinueDays()*0.5);
        }
        //没签到就更新
        else
        {
            mb="0.5";
            userSignIn.setContinueDays(1);
        }
        userSignIn.setUpdateTime(new Date());
        userSignInService.updateSelective(userSignIn);


        //操作积分
        mb=String.valueOf(userSignIn.getContinueDays()*0.5+0.5);
        UserAsset userAsset=new UserAsset();
        userAsset = userAssetService.findList(userAsset).get(0);
        BigDecimal mbOld=new BigDecimal(userAsset.getMbPoint());
        BigDecimal mbNew = mbOld.add(new BigDecimal(mb));
        userAsset.setMbPoint(mbNew.toString());
        userAssetService.updateSelective(userAsset);

        return new Result<>(true,StatusCode.OK,"签到成功",userSignIn);
    }

}
